﻿using Furion.LinqBuilder;
using OAS.Application.ReimburseApp.Dtos;
using OAS.Application.System.Dtos;
using OAS.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace OAS.Application.ReimburseApp.Services
{
    public class ReimburseService : Repository<Reimburse>, IReimburseService, ITransient
    {
        public async Task<List<Reimburse>> GetAllReimburse()
        {
            return await base.GetListAsync();
        }
        public async Task<object> GetReimburseList(DtoQuery dto)
        {
            dto.Time = dto.Time == null ? null : TimeZoneInfo.ConvertTimeFromUtc((DateTime)dto.Time, TimeZoneInfo.Local);
            Expression<Func<Reimburse, bool>> sqlWhere = Expressionable.Create<Reimburse>()
                    .AndIF(!string.IsNullOrEmpty(dto.UserName), o => o.UserName.Equals(dto.UserName))
                    .AndIF(dto.Time != null, o => o.ReimburseTime.Value.Date.Equals(dto.Time.Value.Date))
                    .AndIF(UserContext.CurrentUser.RoleName != "总经理" && !UserContext.IsAdmin, o => o.UserName == UserContext.CurrentUser.UserName)
                    .ToExpression();
            RefAsync<int> total = 0;
            var list = await base.AsQueryable().Where(sqlWhere).ToPageListAsync(dto.PageIndex, dto.PageSize, total);
            return new { list, total };
        }
        public async Task AddReimburse(Reimburse reimburse)
        {
            reimburse.ReimburseTime = reimburse.ReimburseTime == null ? null : TimeZoneInfo.ConvertTimeFromUtc((DateTime)reimburse.ReimburseTime, TimeZoneInfo.Local);
            reimburse.AuditStatus = 0;
            await base.InsertAsync(reimburse);
        }
        public async Task UpdateReimburse(Reimburse reimburse)
        {
            reimburse.ReimburseTime = reimburse.ReimburseTime == null ? null : TimeZoneInfo.ConvertTimeFromUtc((DateTime)reimburse.ReimburseTime, TimeZoneInfo.Local);
            await base.UpdateAsync(reimburse);
        }
        public async Task DeleteReimburse(string[] ids)
        {
            await base.DeleteByIdsAsync(ids);
        }
        public async Task AuditReimburse(DtoAudit dto)
        {
            await base.AsUpdateable().SetColumns(o => new Reimburse { AuditStatus = dto.AuditStatus, Auditor = UserContext.UserName, AuditTime = DateTime.Now })
                   .Where(o => dto.Ids.Contains(o.Id.ToString())).ExecuteCommandAsync();
        }
    }
}
